local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    Asset( "IMAGE", "images/map_icons/npc_item_treasure_map.tex" ),  -- 背包贴图
    Asset( "ATLAS", "images/map_icons/npc_item_treasure_map.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_treasure_map"] or {}
end
local function CreateMapTarget(inst,doer)

	local function getrandomposition(caster, teleportee, target_in_ocean)
		if teleportee == nil then
			teleportee = caster
		end

        if target_in_ocean then
            local pt = TheWorld.Map:FindRandomPointInOcean(20)
            if pt ~= nil then
                return pt
            end
            local from_pt = teleportee:GetPosition()
            local offset = FindSwimmableOffset(from_pt, math.random() * 2 * PI, 90, 16)
                            or FindSwimmableOffset(from_pt, math.random() * 2 * PI, 60, 16)
                            or FindSwimmableOffset(from_pt, math.random() * 2 * PI, 30, 16)
                            or FindSwimmableOffset(from_pt, math.random() * 2 * PI, 15, 16)
            if offset ~= nil then
                return from_pt + offset
            end
            return teleportee:GetPosition()
        else
            local centers = {}
            for i, node in ipairs(TheWorld.topology.nodes) do
                if TheWorld.Map:IsPassableAtPoint(node.x, 0, node.y) and node.type ~= NODE_TYPE.SeparatedRoom then
                    table.insert(centers, {x = node.x, z = node.y})
                end
            end
            if #centers > 0 then
                local pos = centers[math.random(#centers)]
                return Point(pos.x, 0, pos.z)
            else
                return caster:GetPosition()
            end
        end
    end
    ----------------------------------------------------------------------------------------
	local pos = inst.components.npc_everything_data:Get("Point")
	local reason = nil
	if pos == nil then
		pos = getrandomposition(doer)
        local box = SpawnPrefab("npc_item_treasure_box")
        box.Transform:SetPosition(pos.x,0,pos.z)
        box.npc_everything_data:Set("Map_ID",inst.npc_everything_data:Get("Map_ID"))
        inst.npc_everything_data:Set("Point",pos)
        box:AddTag("box_"..inst.npc_everything_data:Get("Map_ID"))  -------- 给宝箱上ID TAG
        if doer.npc_everything_data:Get("npc_event_treasure_map.first_open_announce") ~= true or math.random(100) < 20 then
            doer.npc_everything_data:Set("npc_event_treasure_map.first_open_announce",true) 
            doer.npc_base_lib:Wisper(GetStringsTable()["first_open_announce"])
        end
	end
	-- print(pos.x,pos.y,pos.z)
	-- PutArrowInGround(doer,pos)
    local box_inst = TheSim:FindFirstEntityWithTag("box_"..inst.npc_everything_data:Get("Map_ID")) 
    if box_inst then
        box_inst:PushEvent("Show_Mark")
    else
        print("npc_item_treasure_map error : box_inst is nil")
    end
    ----------------------------------------------------------------------------------------
	return pos, reason
end
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
	inst.entity:AddNetwork()
	inst.entity:AddMiniMapEntity()

    inst.entity:AddAnimState()
    inst.AnimState:SetBank("stash_map")
    inst.AnimState:SetBuild("stash_map")
    inst.AnimState:PlayAnimation("idle")

	-- inst.MiniMapEntity:SetIcon("messagebottletreasure_marker.png")   --- 地图上的红叉
	inst.MiniMapEntity:SetIcon("npc_item_treasure_map.tex") 
	inst:AddTag("treasure_map")
	inst:AddTag("map")
	inst:AddTag("nosteal")
	inst:AddTag("irreplaceable")        ---- 不给带出当前世界
	inst:AddTag("nonpotatable")         ---- 不会腐烂？！     

	MakeInventoryPhysics(inst)
    MakeInventoryFloatable(inst, "med", nil, 0.75)

    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")


    inst:AddComponent("inspectable") --可检查组件
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:ChangeImageName("stash_map")
	inst.components.inventoryitem.cangoincontainer = true
    inst:AddComponent("named")
    -- inst.components.named:SetName(tostring(_table.Name))
    inst.components.named:SetName(GetStringsTable().map_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_treasure_map")] = GetStringsTable().inspect_str  --人物检查的描述

    inst:DoTaskInTime(0,function()
        if inst.npc_everything_data:Get("Map_ID") == nil then
            inst.npc_everything_data:Set("Map_ID", "treasure_map_"..tostring(os.date("%Y-%m-%d_%H:%M:%S")).."_"..tostring(TheWorld.npc_everything_data:Add("treasure_map.num",1) )  )
        end
        inst:AddTag(inst.npc_everything_data:Get("Map_ID")) --------- 藏宝图唯一ID 成为TAG
        print("info  treasure_map id:",inst.npc_everything_data:Get("Map_ID"))
    end)



    inst:AddComponent("mapspotrevealer")
	-- inst.components.mapspotrevealer:SetGetTargetFn(getrevealtargetpos)
	inst.components.mapspotrevealer:SetGetTargetFn(CreateMapTarget)


    return inst
end

return Prefab("npc_item_treasure_map", fn,assets)